package com.csust.automaticteach.utils.template.generate;

import cn.hutool.core.collection.CollUtil;
import com.csust.automaticteach.app.student.ExerciseGenerateApp;
import com.csust.automaticteach.model.enums.GenerateTypeEnum;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.dto.generatequestion.AddGenerateQuestionRequest;
import com.csust.automaticteach.model.entity.Exercises;
import com.csust.automaticteach.model.entity.ExercisesSets;
import com.csust.automaticteach.model.entity.SetExercises;
import com.csust.automaticteach.model.vo.generatequestion.GenerateQuestionResponse;
import com.csust.automaticteach.service.ExercisesService;
import com.csust.automaticteach.service.ExercisesSetsService;
import com.csust.automaticteach.service.SetExercisesService;
import com.csust.automaticteach.utils.ParseGenerateExerciseJsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.hutool.json.JSONUtil.toJsonStr;

/**
 * @ClassName GenerateQuestion
 * @Description question生成器
 * @Author钰玟
 * @Date 2025/7/24 下午12:00
 * @Version 1.0
 **/

@Service
public class QuestionGenerator extends AbstractGeneratorTemplate<AddGenerateQuestionRequest, GenerateQuestionResponse> {
    private final ExerciseGenerateApp exerciseGenerateApp;
    private final ParseGenerateExerciseJsonUtil parseGenerateExerciseJsonUtil;

    // 构造函数注入所有依赖
    public QuestionGenerator(ExercisesService exercisesService,
                             ExercisesSetsService exercisesSetsService,
                             SetExercisesService setExercisesService,
                             ExerciseGenerateApp exerciseGenerateApp,
                             ParseGenerateExerciseJsonUtil parseGenerateExerciseJsonUtil) {
        super(exercisesService, exercisesSetsService, setExercisesService);
        this.exerciseGenerateApp = exerciseGenerateApp;
        this.parseGenerateExerciseJsonUtil = parseGenerateExerciseJsonUtil;
    }


    @Override
    protected void prepareGenerateParams() {
        // 利用基类属性构建标题
        this.title = request.getKnowledgePoint() + "练习题";
    }

    @Override
    protected String callGenerateService() {
        return exerciseGenerateApp.doGenerate(request, chatId);
    }

    @Override
    protected List<GenerateQuestionResponse> parseResult(String result) {
        return parseGenerateExerciseJsonUtil.parseJson(result, GenerateQuestionResponse.class);
    }

    @Override
    protected List<Exercises> convertToExercises(List<GenerateQuestionResponse> responseList) {
        // 从基类获取共同属性
        String type = request.getExams().get(0).getType();
        String knowledgePoint = request.getKnowledgePoint();

        return responseList.stream()
                .map(questionResponse -> {
                    Exercises exercises = new Exercises();
                    BeanUtils.copyProperties(questionResponse, exercises);
                    exercises.setOptions(toJsonStr(questionResponse.getOptions()));
                    exercises.setUserId(userId);
                    exercises.setTitle(title);
                    exercises.setType(type);

                    // 处理知识点
                    List<String> knowledgePoints = questionResponse.getKnowledgePoints();
                    exercises.setKnowledgePoint(CollUtil.isNotEmpty(knowledgePoints)
                            ? knowledgePoints.get(0)
                            : knowledgePoint);

                    return exercises;
                })
                .toList();
    }

    @Override
    protected Long saveData(List<Exercises> exercisesList) {
        // 保存练习题
        boolean saved1 = super.getExercisesService().saveBatch(exercisesList);
        // 创建套题
        ExercisesSets exercisesSets = ExercisesSets.builder()
                .title(title)
                .userId(userId)
                .setType(GenerateTypeEnum.QUESTION.getValue())
                .build();
        boolean saved2 = super.getExercisesSetsService().save(exercisesSets);
        // 建立关联
        List<SetExercises> setExercisesList = super.getSetExercisesService()
                .getSetExercisesList(exercisesList, exercisesSets.getId());
        boolean saved3 = super.getSetExercisesService().saveBatch(setExercisesList);
        // 校验
        ThrowUtils.throwIf(!saved1 || !saved2 || !saved3, ErrorCode.OPERATION_ERROR);
        return exercisesSets.getId();
    }
}
